﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace wfaFoxMigrator
{
    public partial class frmImportTables : Form
    {       
        public frmImportTables()
        {
            InitializeComponent();
        }

        private void btnLoadFile_Click(object sender, EventArgs e)
        {
            loadXmlFile();
        }

        private void loadXmlFile()
        {
            string xmlPath = string.Empty;
            OpenFileDialog fileDlg = new OpenFileDialog();
            fileDlg.Filter = "XML|*.xml";
            if (fileDlg.ShowDialog() == DialogResult.OK)
            {
                xmlPath = fileDlg.FileName;
                dsXml.ReadXml(xmlPath);
                DataTable dt = new DataTable();
                dt = dsXml.Tables[0];
                dataGridView1.DataSource = dt;
                btnImport.Enabled = dt.Rows.Count > 0;
            }
            else
            {
                btnImport.Enabled = false;
            }
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            string msgsError = "The following errors was found in the process: \n";
            string msgsSucss = "The following tables was created successfully: \n";            
            SortedList<string,string> checkedScripts = new SortedList<string,string>();
            foreach (DataGridViewRow dataGridRow in dataGridView1.Rows)
            {               
                if (Convert.ToBoolean(dataGridRow.Cells["Include"].Value))
                {
                    checkedScripts.Add(dataGridRow.Cells["TableName"].Value.ToString(), dataGridRow.Cells["Script"].Value.ToString());                    
                }                
            }
            foreach (KeyValuePair<string,string> kvp in checkedScripts)
            {                         
                using (DataFunctions dfcs = new DataFunctions { ConnectionString = CommonFunctions.GetCurrentCSDest() })
                {
                    string strSql = kvp.Value;
                    if (!dfcs.ExecuteNonQueryOracle(ref strSql))
                    {
                        msgsError += "Error processing Table: " + kvp.Key + ", details: " + strSql + "\n";
                    }
                    else
                    {
                        msgsSucss +=  "Table " + kvp.Key + " already processed \n";
                    }
                }
            }
            msgsError += "\n ------------------------------------------------- \n";
            msgsSucss += "\n ------------------------------------------------- \n";
            txtLog.Text = msgsError + msgsSucss;
            txtLog.WordWrap = true;
            txtLog.SelectionStart = txtLog.Text.Length; 
            txtLog.ScrollToCaret(); 
            txtLog.Refresh();
            MessageBox.Show(Properties.Settings.Default.ProcessFinished);
        }

        private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow dataGridRow in dataGridView1.Rows)
            {
                dataGridRow.Cells["Include"].Value = true;                
            }
        }

        private void unSelectAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow dataGridRow in dataGridView1.Rows)
            {
                dataGridRow.Cells["Include"].Value = false;
            }
        }

        private void copyAllTableNamesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            List<string> checkedTables = new List<string>();            
            foreach (DataGridViewRow dataGridRow in dataGridView1.Rows)
            {
                if (Convert.ToBoolean(dataGridRow.Cells["Include"].Value))
                {
                    checkedTables.Add(dataGridRow.Cells["TableName"].Value.ToString());
                }
            }
            string values = String.Join(",",checkedTables);
            if (!String.IsNullOrEmpty(values))
            {
                txtLog.Text = string.Empty;
                txtLog.Text = values;
                MessageBox.Show(Properties.Settings.Default.TableNamesToCB);
            }
        }

        private void generateAllTableDropsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            List<string> checkedTables = new List<string>();
            foreach (DataGridViewRow dataGridRow in dataGridView1.Rows)
            {
                if (Convert.ToBoolean(dataGridRow.Cells["Include"].Value))
                {
                    checkedTables.Add("DROP TABLE " + dataGridRow.Cells["TableName"].Value.ToString() + ";\n");
                }
            }
            string values = String.Join(string.Empty, checkedTables);
            if (!String.IsNullOrEmpty(values))
            {
                txtLog.Text = string.Empty;
                txtLog.Text = values;
                MessageBox.Show(Properties.Settings.Default.TableNamesToCB);
            }
        }
    }
}
